﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.Data;

namespace V5_EShop.Admin.Article {
    public partial class Admin_ArticleComment : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Bind_DataList();
            }
        }
        #region 增删改
        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = V5.SQL.Query_Pager("*", "Tbl_ArticleComment", string.Empty, string.Empty, Pager.PageSize, Pager.CurrentPageIndex, ref RecordCount);
            Pager.RecordCount = RecordCount;

            this.gvList.DataSource = dt.DefaultView;
            this.gvList.DataBind();
        }
        #endregion

        protected void Pager_PageChanged(object sender, EventArgs e) {
            Bind_DataList();
        }

        public string Bind_IsShow(object o) {
            string temp = "";
            if (Easp.Str.SetString(o) == "") {
                return "";
            }
            switch (Easp.Str.SetString(o)) {
                case "0":
                    temp = "<span style='color:red'>禁用</span>";
                    break;
                case "1":
                    temp = "<span style='color:green'>通过</span>";
                    break;
                case "3":
                    temp = "待审";
                    break;
            }
            return temp;
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            DataTable dt = V5.SQL.Query_By("a.*,b.Title", @"(
                    Select * From Tbl_ArticleComment Where ID = " + id + @"
                ) a 
                left join Tbl_Article b 
                on
                a.ArticleID=b.ID", "", "a.ID desc", 0);
            if (dt != null && dt.Rows.Count > 0) {
                lblArticleTitle.Text = dt.Rows[0]["Title"].ToString();
                lblCommentName.Text = dt.Rows[0]["CommentName"].ToString();
                lblCommentContent.Text = dt.Rows[0]["CommentContent"].ToString();
                txtReplayContent.Text = dt.Rows[0]["ReplayContent"].ToString();
                lblCommentIP.Text = dt.Rows[0]["CommentIP"].ToString();
                lblCommentTime.Text = dt.Rows[0]["AddDateTime"].ToString();
                rbtnPassed.SelectedIndex = rbtnPassed.Items.IndexOf(rbtnPassed.Items.FindByValue(dt.Rows[0]["IsShow"].ToString()));
                hfID.Value = dt.Rows[0]["ID"].ToString();
            }
        }
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            int selectID = Easp.Str.SetNumber(e.CommandArgument);
            string eName = Easp.Str.SetString(e.CommandName);
            switch (eName) {
                case "go":
                    Bind_DataEdit(selectID);
                    break;
                case "del":
                    Bind_DataDelete(selectID);
                    break;
            }
        }

        private void Bind_DataDelete(int id) {
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int ID = InputHelper.SetNumber(this.hfID.Value);
            string ReplayContent = this.txtReplayContent.Text;
            int IsShow = InputHelper.SetNumber(this.rbtnPassed.SelectedValue);
            this.Bind_DataList();
        }

        public DataRow Bind_Article(object ArticleID) {
            int ID = Easp.Str.SetNumber(ArticleID);
            return V5.SQL.Query_By("*", "Tbl_Article", " ID=" + ID, "", 1).Rows[0];
        }
    }
}